// 2013-9-12上午12:08:40  net.betou.trade.main.dao.role.impl.AgencyRoleDaoImpl.java

package net.betou.trade.main.dao.role.impl;

import java.util.List;

import net.betou.common.hibernate3.Finder;
import net.betou.common.page.Pagination;
import net.betou.trade.entity.main.Role;
import net.betou.trade.main.dao.role.AgencyRoleDao;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

/**
 * 
 * @author 叶装装
 */
@Repository
public class AgencyRoleDaoImpl extends DaoImpl implements AgencyRoleDao{

	public Pagination getPage( Integer level, Integer selfId, String searchProperty, String searchValue, String orderProperty, String orderDirection, int pageSize, int pageNo ) {
		return getPage( level, selfId, searchProperty, searchValue, orderProperty, orderDirection, pageSize, pageNo, null );
	}
	
	public Pagination getPage( Integer level, Integer selfId, String searchProperty, String searchValue, String orderProperty, String orderDirection, int pageSize, int pageNo, String hideIdentity ){
		
		
		Finder f = Finder.create( "from Role bean where bean.manager=:manager " );
		f.setParam( "manager", false );

		if( selfId != null ){
			f.append( " and bean.admin.id=:selfId" );
			f.setParam( "selfId", selfId );
		}

		if( level != null ){
			f.append( " and bean.level >=:level " );
			f.setParam( "level", level );
		}
		
		// 不显示机构数据
		if( hideIdentity != null  ){
			f.append( " and ( bean.identity != :identity  or bean.identity is null )" );
			f.setParam( "identity", hideIdentity  );
		}

		createSearch( f, searchProperty, searchValue );
		createOrder( f, orderProperty, orderDirection, "id" );

		return this.find( f, pageNo, pageSize );
		
		
	}

	public Role findBySelf( Integer selfId, Integer id ) {
		String hql = "from Role bean where bean.admin.id =:adminId and bean.id=:id";

		Query query = getSession( ).createQuery( hql );
		query.setParameter( "adminId", selfId );
		query.setParameter( "id", id );

		return ( Role ) query.uniqueResult( );
	}

	@SuppressWarnings( "unchecked" )
	public List< Role > findList( Integer level, Integer selfId ) {
		Finder f = Finder.create( "from Role bean where bean.admin.id=:id " );
		f.setParam( "id", selfId );

		if( level != null ){
			f.append( " and bean.level >=:level " );
			f.setParam( "level", level );
		}

		return find( f, true );
	}
	
	@SuppressWarnings( "unchecked" )
	public List< Role > findListNotDefault( Integer level, Integer selfId ){
		Finder f = Finder.create( "from Role bean where bean.admin.id=:id and  bean.identity is null" );
		f.setParam( "id", selfId );

		if( level != null ){
			f.append( " and bean.level >=:level " );
			f.setParam( "level", level );
		}
		return find( f, true );
	}
	
	public Role findByIdentity( String identity ){
		String hql = "from Role bean where bean.identity =?";
		return ( Role )findUniqueCacheable( hql, identity );
	}
	
	public Role findDefaultBySelf( Integer selfId, String identity ){
		String hql = "from Role bean where bean.admin.id=? and bean.identity=?";
		return ( Role )findUnique( hql, selfId, identity );
	}
	
	@SuppressWarnings( "unchecked" )
	public List< Role > findListByAllowInit( Boolean allowRead ){
		Finder f = Finder.create( "from Role bean where bean.manager=:manager and bean.allowInit=:allowInit" );
		f.setParam( "manager", true );
		f.setParam( "allowInit", true );
		
		if( allowRead != null ){
			f.append( " and bean.allowRead=:allowRead" );
			f.setParam( "allowRead", allowRead );
		}
		
		
		return find( f, true );
	}

}
